package com.example.zhu95.mytimber.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.activity.MainActivity;
import com.example.zhu95.mytimber.adapters.AlbumGridAdapter;
import com.example.zhu95.mytimber.adapters.AlbumLinearAdapter;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.listener.AlbumClickListener;
import com.example.zhu95.mytimber.ui.AlbumsSectionTitleIndicator;
import com.example.zhu95.mytimber.utils.MyComparator;

import java.util.Collections;
import java.util.List;

import xyz.danoz.recyclerviewfastscroller.vertical.VerticalRecyclerViewFastScroller;

import static com.example.zhu95.mytimber.ConstantValue.CURRENT_VIEW_MODE;
import static com.example.zhu95.mytimber.ConstantValue.GRID_VIEW;
import static com.example.zhu95.mytimber.ConstantValue.LINEAR_VIEW;

/**
 * @author zhu95
 */
public class AlbumFragment extends Fragment {
    int currentViewMode = GRID_VIEW;
    AlbumClickListener albumClickListener;
    RecyclerView recyclerViewAlbums;
    AlbumGridAdapter albumGridAdapter = null;
    GridLayoutManager gridLayoutManager;
    AlbumLinearAdapter albumLinearAdapter = null;
    LinearLayoutManager linearLayoutManager;
    VerticalRecyclerViewFastScroller scroller;
    AlbumsSectionTitleIndicator indicator;
    List<AlbumBean> albums = SongData.albums;

    public List<AlbumBean> getAlbums() {
        return albums;
    }

    public void setAlbums(List<AlbumBean> albums) {
        this.albums = albums;
        notifyDataSetChanged();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivity mainActivity = (MainActivity) getActivity();
        albumClickListener = mainActivity.getAlbumClickListener();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_library_album, container, false);
        Context context = view.getContext();
        recyclerViewAlbums = view.findViewById(R.id.recycler_view_album);
        scroller = view.findViewById(R.id.scroller_albums);
        indicator = view.findViewById(R.id.indicator_album);
        if (albumGridAdapter == null) {
            albumGridAdapter = new AlbumGridAdapter(albums, albumClickListener);
        }
        gridLayoutManager = new GridLayoutManager(context, 2);
        if (albumLinearAdapter == null) {
            albumLinearAdapter = new AlbumLinearAdapter(albums, albumClickListener);
        }
        linearLayoutManager = new LinearLayoutManager(context);

        scroller.setRecyclerView(recyclerViewAlbums);
        recyclerViewAlbums.setOnScrollListener(scroller.getOnScrollListener());
        scroller.setSectionIndicator(indicator);
        setRecyclerViewMode(currentViewMode);
        return view;
    }

    public void setRecyclerViewMode(int viewMode) {

        if (viewMode == LINEAR_VIEW) {
            scroller.setVisibility(View.VISIBLE);
            indicator.setVisibility(View.VISIBLE);
            currentViewMode = viewMode;
            recyclerViewAlbums.setAdapter(albumLinearAdapter);
            recyclerViewAlbums.setLayoutManager(linearLayoutManager);
        } else if (viewMode == GRID_VIEW) {
            scroller.setVisibility(View.GONE);
            indicator.setVisibility(View.GONE);
            currentViewMode = viewMode;
            recyclerViewAlbums.setAdapter(albumGridAdapter);
            recyclerViewAlbums.setLayoutManager(gridLayoutManager);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle args = getArguments();
        if (args != null) {
            int viewMode = args.getInt(CURRENT_VIEW_MODE);
            if (currentViewMode == viewMode) {
                return;
            }
            setRecyclerViewMode(viewMode);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Bundle args = new Bundle();
        args.putInt(CURRENT_VIEW_MODE, currentViewMode);
        setArguments(args);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        try {
            switch (item.getItemId()) {
                case R.id.sort_az:
                    Collections.sort(albums, new MyComparator(AlbumBean.class.getDeclaredField("album")));
                    break;
                case R.id.sort_za:
                    Collections.sort(albums, new MyComparator(AlbumBean.class.getDeclaredField("album"), true));
                    break;
                case R.id.sort_year:
                    Collections.sort(albums, new MyComparator(AlbumBean.class.getDeclaredField("firstYear")));
                    break;
                case R.id.sort_artist:
                    Collections.sort(albums, new MyComparator(AlbumBean.class.getDeclaredField("artist")));
                    break;
                case R.id.sort_songnum:
                    Collections.sort(albums, new MyComparator(AlbumBean.class.getDeclaredField("songNum"), true));
                    break;
                default:
            }
            // notifyDataSetChanged();
            albumLinearAdapter.notifyDataSetChanged();
            albumGridAdapter.notifyDataSetChanged();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return super.onOptionsItemSelected(item);
    }


    public void notifyDataSetChanged() {
        albumLinearAdapter.setAlbums(albums);
        albumLinearAdapter.notifyDataSetChanged();
        albumGridAdapter.setAlbums(albums);
        albumGridAdapter.notifyDataSetChanged();
    }
}
